package cn.tedu.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

@Component
public class LogGatewayFilterFactory extends
        AbstractGatewayFilterFactory<LogGatewayFilterFactory.Config> {
//构造器初始化
    public LogGatewayFilterFactory() {
        super(Config.class);
    }

    public List<String> shortcutFieldOrder() {

        return Arrays.asList("cacheLog", "consoleLog");
    }


    //实现过滤器具体逻辑
    @Override
    public GatewayFilter apply(Config config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange,
                                     GatewayFilterChain chain) {
                if (config.getCacheLog()){
//                    if (config.isConsoleLog()) {
                    System.out.println("缓存日志开启");
                }
                if (config.getConsoleLog()){
                    System.out.println("控制台日志开启");
                }
                return chain.filter(exchange);
            }
        };
    }

    //内部类 参数定义
    //缓存日志   控制台日志  是否开始 true  fase
    public static class Config{
        private Boolean cacheLog;
        private Boolean consoleLog;


        public Boolean getCacheLog() {
            return cacheLog;
        }

        public void setCacheLog(Boolean cacheLog) {
            this.cacheLog = cacheLog;
        }

        public Boolean getConsoleLog() {
            return consoleLog;
        }

        public void setConsoleLog(Boolean consoleLog) {
            this.consoleLog = consoleLog;
        }
    }
}
